/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.financial.service.lettercredit;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.financial.entity.lettercredit.LetterCredit;
import com.jeesite.modules.financial.dao.lettercredit.LetterCreditDao;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.financial.entity.lettercredit.LetterCreditDetail;
import com.jeesite.modules.financial.dao.lettercredit.LetterCreditDetailDao;

/**
 * 信用证Service
 * @author Kilo
 * @version 2021-03-09
 */
@Service
@Transactional(readOnly=true)
public class LetterCreditService extends CrudService<LetterCreditDao, LetterCredit> {
	
	@Autowired
	private LetterCreditDetailDao letterCreditDetailDao;
	
	/**
	 * 获取单条数据
	 * @param letterCredit
	 * @return
	 */
	@Override
	public LetterCredit get(LetterCredit letterCredit) {
		LetterCredit entity = super.get(letterCredit);
		if (entity != null){
			LetterCreditDetail letterCreditDetail = new LetterCreditDetail(entity);
			letterCreditDetail.setStatus(LetterCreditDetail.STATUS_NORMAL);
			entity.setLetterCreditDetailList(letterCreditDetailDao.findList(letterCreditDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param letterCredit 查询条件
	 * @param letterCredit.page 分页对象
	 * @return
	 */
	@Override
	public Page<LetterCredit> findPage(LetterCredit letterCredit) {
		return super.findPage(letterCredit);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param letterCredit
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(LetterCredit letterCredit) {
		super.save(letterCredit);
		// 保存上传附件
		FileUploadUtils.saveFileUpload(letterCredit.getId(), "letterCredit_file");
		// 保存 LetterCredit子表
		for (LetterCreditDetail letterCreditDetail : letterCredit.getLetterCreditDetailList()){
			if (!LetterCreditDetail.STATUS_DELETE.equals(letterCreditDetail.getStatus())){
				letterCreditDetail.setLetterCreditId(letterCredit);
				if (letterCreditDetail.getIsNewRecord()){
					letterCreditDetailDao.insert(letterCreditDetail);
				}else{
					letterCreditDetailDao.update(letterCreditDetail);
				}
			}else{
				letterCreditDetailDao.delete(letterCreditDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param letterCredit
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(LetterCredit letterCredit) {
		super.updateStatus(letterCredit);
	}
	
	/**
	 * 删除数据
	 * @param letterCredit
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(LetterCredit letterCredit) {
		super.delete(letterCredit);
		LetterCreditDetail letterCreditDetail = new LetterCreditDetail();
		letterCreditDetail.setLetterCreditId(letterCredit);
		letterCreditDetailDao.deleteByEntity(letterCreditDetail);
	}
	
}